package com.devframe.srtp.server.file;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.devframe.srtp.Account;
import com.devframe.srtp.SRTPResult;
import com.devframe.srtp.server.SRTPServerContainer;
import com.devframe.srtp.server.parse.SRTPParam;
import com.devframe.srtp.util.common.SRTPJSONUtil;
import com.devframe.srtp.util.enumobj.EnumSRTPAction;

public class SRTPFileHandlerFactory {
	private static Log log=LogFactory.getLog(SRTPFileHandlerFactory.class);
	
	public static String process(SRTPParam requestParams){
		Account account=SRTPServerContainer.getInstance().getAccount(requestParams.getUserName());
		EnumSRTPAction action=EnumSRTPAction.converByValue(requestParams.getAction());
		SRTPResult result=null;
		if(account.auth(requestParams.getPwd())){
			if(EnumSRTPAction.UPLOAD_FILE==action){//文件上传
				if(requestParams.getFileUpload()!=null){
					SRTPStoreFileHandler handler=new SRTPStoreFileHandler(account);
					result=handler.process(requestParams);
				}			
			}else if(EnumSRTPAction.DELETE_FILE==action){//文件删除
				if(StringUtils.isNotBlank(requestParams.getFilePath())){
					SRTPDeleteFileHandler handler=new SRTPDeleteFileHandler(account);
					result=handler.process(requestParams);
				}
			}else if(EnumSRTPAction.REPLACE_FILE==action){//文件替换
				if(requestParams.getFileUpload()!=null &&StringUtils.isNotBlank(requestParams.getFilePath())){
					SRTPReplaceFileHandler handler=new SRTPReplaceFileHandler(account);
					result=handler.process(requestParams);
				}
			}else if(EnumSRTPAction.CREATE_THUMB_PICTURE==action){//缩略图处理
				if(StringUtils.isNotBlank(requestParams.getFilePath())){
					SRTPCreateThumbPictureHandler handler=new SRTPCreateThumbPictureHandler(account);
					result=handler.process(requestParams);
				}
			}
		}else{
			result=new SRTPResult();
			result.setAction(EnumSRTPAction.NULL.getValue());
			result.setCode(false);
			result.setMsg("密码错误");
		}
		if(result==null){
			result=new SRTPResult();
			result.setAction(EnumSRTPAction.NULL.getValue());
			result.setCode(false);
			result.setMsg("无效动作");
		}
		
		String json=SRTPJSONUtil.toJSONString(result);
		if(log.isDebugEnabled()){
			log.debug("执行结果:"+json);
		}
		return json;
	}
}
